package com.igoal.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Entity
@Table(name="user_tag")
public class UserTag extends BaseObject implements Audit, ItemOwner {

	private static final long serialVersionUID = 3975417242691791380L;
	
	private Long id;
	private Long componentId;
	private ComponentEnum component;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;
	
	private Tag tag;
	private User user;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name="tag_id")
	public Tag getTag() {
		return tag;
	}

	public void setTag(Tag tag) {
		this.tag = tag;
	}

	@ManyToOne
	@JoinColumn(name="user_id")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Column(name="component_id")
	public Long getComponentId() {
		return componentId;
	}

	public void setComponentId(Long componentId) {
		this.componentId = componentId;
	}

	@Enumerated(EnumType.STRING)
	@Column(name="component")
	public ComponentEnum getComponent() {
		return component;
	}

	public void setComponent(ComponentEnum component) {
		this.component = component;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(component.toString()).append(" is tagged as ").append(tag.getTagName()).append(" by ").append(user.getUsername());
		return sb.toString();
	}

	@Override
	public boolean equals(Object o) {
		if(o != null && o instanceof UserTag){
			UserTag that = (UserTag) o;
			return id.equals(that.id) && user.equals(that.user) && tag.equals(that.tag) && componentId.equals(that.componentId) && component.equals(that.component);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	@Transient
	public User getOwner() {
		return user;
	}

}
